/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hanuma;

import java.util.*;
import java.util.regex.*;

/**
 *
 * @author niyaz
 */
public class UniversalFilter implements Filter {

    private BagOfWords words;

    public UniversalFilter(BagOfWords words) {
        this.words = words;
    }

    public double relevance(Person person) {
        double buf = 0;
        Iterator<String> iter = words.words.keySet().iterator();
        while (iter.hasNext()) {
            String str = iter.next();
            buf += (countIndexOf(person.toString(),str)) * words.words.get(str);
            //buf += (person.toString().toLowerCase().split(str.toLowerCase()).length - 1) * words.words.get(str);
        }
       
        return buf;
    }

    public int countIndexOf(String text, String search) {
        Matcher m = Pattern.compile(search).matcher(text);
        int count;
        for (count = 0; m.find(); count++);
        return count;
    }
}
